//
//  HomeSectionView.m
//  sensen_ios
//
//  Created by pro on 16/1/30.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "HomeSectionView.h"

@interface HomeSectionView ()

@property (nonatomic, strong) UILabel *titleLabel; // 标题
@property (nonatomic, strong) UIImageView *contentImgView; // 内容图片视图

@end

@implementation HomeSectionView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
    }
    
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame type:(ZKHomeSectionViewType)type {
    if ([super initWithFrame:frame]) {
        // 设置自身默认属性
        self.layer.cornerRadius = 4;
        self.backgroundColor = RED_COLOR;
        self.type = type;
        [self _initViews];
    }
    
    return self;
}
#pragma mark -- 创建视图 --
- (void)_initViews {
    
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = 0;
    CGFloat height = 0;
    
    // 横向
    if (self.type == ZKHomeSectionViewTypeH) {
        width = 100;
        height = 20;
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        _titleLabel.font = B_FONT(16);
        _titleLabel.text = @"未填写标题";
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
    
        y = self.height * 0.2;
        width = self.height * 0.6;
        height = width;
        _contentImgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [_contentImgView setContentMode:UIViewContentModeScaleAspectFit];
        _contentImgView.image = [UIImage imageNamed:@"新闻资讯"];
        [self addSubview:_contentImgView];
        
        // 设置图片的位置
        _contentImgView.right = self.width / 2.4;
        // 设置标题的位置
        _titleLabel.left = _contentImgView.right + 10;
        _titleLabel.center = CGPointMake(_titleLabel.center.x, self.height / 2);
    }
    
    // 纵向文字在上
    else if (_type == ZKHomeSectionViewTypeV) {
        y = 10;
        width = 100;
        height = 20;
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        _titleLabel.font = B_FONT(16);
        _titleLabel.text = @"未填写标题";
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
        
        y = _titleLabel.bottom + 5;
        width = MIN(self.width, self.height) * 0.6;
        height = width;
        _contentImgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [_contentImgView setContentMode:UIViewContentModeScaleAspectFit];
        _contentImgView.image = [UIImage imageNamed:@"AQ鱼保姆"];
        [self addSubview:_contentImgView];
        
        // 设置标题的位置
        _titleLabel.top = (self.height - _titleLabel.height - _contentImgView.height - 5) / 2.0;
        _titleLabel.center = CGPointMake(self.width / 2.0, _titleLabel.center.y);
        // 设置图片的位置
        _contentImgView.top = _titleLabel.bottom + 5;
        _contentImgView.center = CGPointMake(self.width / 2.0, _contentImgView.center.y);

    } else {
        
        y = 10;
        width = MIN(self.width, self.height) * 0.4;
        height = width;
        _contentImgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [_contentImgView setContentMode:UIViewContentModeScaleAspectFit];
        _contentImgView.image = [UIImage imageNamed:@"AQ鱼保姆"];
        [self addSubview:_contentImgView];
        
        y = _contentImgView.bottom + 10;
        width = 100;
        height = 20;
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        _titleLabel.font = B_FONT(16);
        _titleLabel.text = @"未填写标题";
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
        
        // 设置标题的位置
        _contentImgView.top = (self.height - _titleLabel.height - _contentImgView.height - 10) / 2.0;
        _contentImgView.center = CGPointMake(self.width / 2.0, _contentImgView.center.y);
        // 设置图片的位置
        _titleLabel.top = _contentImgView.bottom + 10;
        _titleLabel.center = CGPointMake(self.width / 2.0, _titleLabel.center.y);
    }
}

#pragma mark -- 重写set --
- (void)setTitle:(NSString *)title {
    _title = title;
    CGFloat width = [_title getSizeOfBlodString:16].width;
    _titleLabel.width = width;
    _titleLabel.text = _title;

    if (SCREEN_WIDTH<=320) {
      _titleLabel.font = [UIFont systemFontOfSize:12];
    }
    if (self.type == ZKHomeSectionViewTypeH) {
        // 设置标题的位置
        _titleLabel.left = _contentImgView.right + 10;
        _titleLabel.center = CGPointMake(_titleLabel.center.x, self.height / 2);

        
    } else if (self.type == ZKHomeSectionViewTypeV) {
        _titleLabel.center = CGPointMake(self.width / 2.0, _titleLabel.center.y);
    } else {
        _titleLabel.center = CGPointMake(self.width / 2.0, _titleLabel.center.y);
    }
}
- (void)setTitleColor:(UIColor *)titleColor{
    _titleColor = titleColor;
    _titleLabel.textColor = titleColor;
}
- (void)setImgName:(NSString *)imgName {
    _imgName = imgName;
    _contentImgView.image = [UIImage imageNamed:_imgName];
}

@end
